<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/*总数字资源收藏model*/

class ResourceCollect extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_collect';

    /**
     *判断资源是否收藏
     * @param $user_id
     * @param $resource_guid
     */
    public static function isCollect($resource_guid, $user_id)
    {
        $res = self::where('resource_guid', $resource_guid)->where('user_id', $user_id)->value('id');
        return $res;
    }

    /**
     * 收藏与取消收藏
     * @param token  用户token  用户筛选用户是否收藏
     * @param id  id
     */
    public function collectAndCancel($resource_guid, $user_id)
    {
        $is_collect = self::isCollect($resource_guid, $user_id);
        if ($is_collect) {
            $res = $this->where('id', $is_collect)->delete();
            $msg = '移除书架';
        } else {
            $this->user_id = $user_id;
            $this->resource_guid = $resource_guid;
            $res = $this->save();
            $msg = '加入书架';
        }
        return [$res, $msg];
    }

    /**
     * 获取收藏大类型数据
     */
    public function collectTypeList($user_id)
    {
        $res = ResourceType::select(['id', 'type_name', 'img', 'img_click'])->where('is_del', 1)->where('id', '<>', 999)->orderBy('id')->get()->toArray();
        foreach ($res as $key => $val) {
            $res[$key]['number'] = $this->collectNumber($user_id, $val['id']);
        }
        return $res;
    }


    /**
     * 获取用户收藏数量
     * @param user_id 用户id
     * @param big_type_id  资源大类型id  未传表示全部
     */
    public function collectNumber($user_id, $big_type_id = null)
    {
        $res = $this->from($this->getTable() . ' as c')
            ->select('c.id')
            ->join('resource_total as r', 'r.guid', '=', 'c.resource_guid')
            ->where(function ($query) use ($user_id, $big_type_id) {
                if ($user_id) {
                    $query->where('c.user_id', $user_id);
                }
                if ($big_type_id) {
                    $query->where('r.big_type_id', $big_type_id);
                }
            })
            ->where('r.is_del', 1)
            ->count();
        return $res;
    }

    /**
     * 用户收藏列表
     * @param user_id 用户id
     * @param big_type_id  资源大类型id  未传表示全部
     * @param keywords 关键字搜索
     * @param limit 限制条数   默认 10 条
     */
    public function collectList($user_id, $keywords = null, $big_type_id = null, $limit = 10)
    {

        //获取敏感词
                // $sensitiveWordModel = new SensitiveWord();
        // $sensitive_word = $sensitiveWordModel->getSensitiveWordALL();

        $res = $this->from($this->getTable() . ' as c')
            ->select([
                'c.id as collect_id',
                // 'r.id',
                'r.guid',
                'r.resource_business_id',
                'r.title',
                'r.author',
                'r.isbn',
                'r.press',
                'r.pre_time',
                'r.img',
                'r.intro',
                'r.big_type_id',
                'r.type_id',
                'c.create_time',
                't.type_name as big_type_name',
            ])
            ->join('resource_total as r', 'r.guid', '=', 'c.resource_guid')
            ->join('resource_type as t', 't.id', '=', 'r.big_type_id')
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('title', 'like', "%$keywords%");
                }
            })
            ->where(function ($query) use ($user_id, $big_type_id) {
                if ($user_id) {
                    $query->where('c.user_id', $user_id);
                }
                if ($big_type_id) {
                    $query->where('r.big_type_id', $big_type_id);
                }
            })
            // ->where(function ($query) use ($sensitive_word) {
            //     if ($sensitive_word) {
            //         foreach ($sensitive_word as $item) {
            //             $query->where('title', 'not like', "%$item%");
            //         }
            //     }
            // })->where(function ($query) use ($sensitive_word) {
            //     if ($sensitive_word) {
            //         foreach ($sensitive_word as $item) {
            //             $query->where('author', 'not like', "%$item%");
            //         }
            //         $query->orWhereNull('author');
            //     }
            // })
            ->where('is_mask', 1)
            ->where('r.is_del', 1)
            ->orderByDesc('c.id')
            ->paginate($limit)
            ->toArray();
        return $res;
    }

    /**
     * 批量删除收藏书架的书籍
     * @param collect_id 收藏id 多个逗号拼接
     * @param user_id 用户id
     */
    public function batchDelCollectBook($collect_id, $user_id)
    {
        $collect_id = explode(',', $collect_id);
        return $this->whereIn('id', $collect_id)->where('user_id', $user_id)->delete();
    }
}
